<script setup>
import { ref } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import {
  articleDeleteChannelService,
  articleGetChannelsService
} from '@/api/article.js'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelEdit from '@/components/ChannelEdit.vue'

defineOptions({
  name: 'ArticleChannel'
})

// 加载动画
const loading = ref(false)

// 获取文章分类列表
const channelList = ref([])
const getChannelList = async () => {
  loading.value = true
  const result = await articleGetChannelsService()
  channelList.value = result.data.data
  loading.value = false
}
getChannelList()

// 文章分类弹框
const dialog = ref()

// 添加文章分类
const onAddChannelCategory = () => {
  dialog.value.open({})
}

// 编辑文章分类
const onEditChannelCategory = (row) => {
  dialog.value.open(row)
}

// 分类操作成功，重新获取分类
const onSuccess = () => {
  getChannelList()
}

// 删除分类
const onDeleteChannel = async (row) => {
  await ElMessageBox.confirm('确认要删除该分类吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await articleDeleteChannelService(row.id)
  await getChannelList()
  ElMessage.success('分类删除成功！')
}
</script>

<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannelCategory"
        >添加分类</el-button
      >
    </template>
    <template #default>
      <el-table
        v-loading="loading"
        :data="channelList"
        style="width: 100%"
        height="100%"
        :row-style="{ height: '80px' }"
      >
        <el-table-column
          label="序号"
          type="index"
          width="100px"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作" width="100px">
          <template #default="{ row }">
            <el-button
              @click="onEditChannelCategory(row)"
              type="primary"
              circle
              plain
              :icon="Edit"
            ></el-button>
            <el-button
              @click="onDeleteChannel(row)"
              type="danger"
              circle
              plain
              :icon="Delete"
            ></el-button>
          </template>
        </el-table-column>

        <template #empty>
          <el-empty description="暂无文章分类，快去添加分类吧~"></el-empty>
        </template>
      </el-table>

      <!-- 编辑弹框 -->
      <ChannelEdit @success="onSuccess" ref="dialog" title="添加分类">
      </ChannelEdit>
    </template>
  </PageContainer>
</template>

<style lang="scss" scoped></style>
